import random
from PyQt5 import QtCore, QtGui
from PyQt5.QtWidgets import *
from myutil import globalvar as gl, logs
from myutil.api.ac_jwc_userinfo import get_xjxx_dict
from myutil.api.check_cookie import request_test
from myutil.api.get_cookie_api import get_cookie_from_username_passwd
from myutil.api.get_yxkc_json_from_cookie import get_free_course_list_and_yxkc_list
from ui.login_form import Ui_Frame as login_frame


class login_form(QFrame, login_frame):
    def __init__(self, parent=None):
        super(login_form, self).__init__(parent)
        self.setupUi(self)

        # 选择用户密码登录
        self.login_username_radioButton_event()

        self.check_cookie_pushButton.clicked.connect(self.check_cookie_event)
        self.login_cookie_radioButton.clicked.connect(self.login_cookie_radioButton_event)
        self.login_username_radioButton.clicked.connect(self.login_username_radioButton_event)
        self.login_pushButton.clicked.connect(self.login_event)
        self.how_to_find_cookie_pushButton.clicked.connect(self.how_to_find_cookie_event)

        self.user_info_dict = gl.get_value("user_info")
        if self.user_info_dict != None:
            self.login_status_label.setText(
                '<html><head/><body><p align="center"><span style=" color:#00aa00;">已经登录了</span></p><p align="center"><span style=" color:#00aa00;">想切换账号的话请重新登录</span></p></body></html>')

    def how_to_find_cookie_event(self):
        QtGui.QDesktopServices.openUrl(QtCore.QUrl("https://jingyan.baidu.com/article/afd8f4deaf490634e286e936.html"))

    def check_cookie_event(self):
        logs.logs_print("check_cookie_event")
        res = request_test(str(self.cookie_lineEdit.text()))
        self.cookie_status.setText(res)
        if res == "cookie检查成功":
            # 登录按钮可用
            self.login_pushButton.setEnabled(True)
        logs.logs_print(res)

    def login_cookie_radioButton_event(self):
        self.cookie_status.setVisible(True)
        self.how_to_find_cookie_pushButton.setVisible(True)
        self.check_cookie_pushButton.setVisible(True)
        self.cookie_lineEdit.setVisible(True)

        self.username_label.setVisible(False)
        self.username_lineEdit.setVisible(False)
        self.password_lineEdit.setVisible(False)
        self.password_label.setVisible(False)

        # 登录按钮不可用
        self.login_pushButton.setEnabled(False)

    def login_username_radioButton_event(self):
        self.cookie_status.setVisible(False)
        self.how_to_find_cookie_pushButton.setVisible(False)
        self.check_cookie_pushButton.setVisible(False)
        self.cookie_lineEdit.setVisible(False)

        self.username_label.setVisible(True)
        self.username_lineEdit.setVisible(True)
        self.password_lineEdit.setVisible(True)
        self.password_label.setVisible(True)

        # 登录按钮可用
        self.login_pushButton.setEnabled(True)

    def login_event(self):
        logs.logs_print("login_event")

        if self.login_username_radioButton.isChecked():
            logs.logs_print("账号密码登录")
            username = str(self.username_lineEdit.text())
            password = str(self.password_lineEdit.text())
            try:
                get_cookie_res = get_cookie_from_username_passwd(username, password)
                if get_cookie_res == "error":
                    self.login_status_label.setText(
                        '''<html><head/><body><p align="center"><span style=" color:#ff0000;">登录失败<br>请连接vpn或者校园网以后重试</span></p></body></html>''')
                    return 0
                cookie = get_cookie_res
                self.login_pushButton.setEnabled(False)
                self.login_status_label.setText(
                    '''<html><head/><body><p align="center"><span style=" color:#00aa00;">登录成功</span></p></body></html>''')
                xjxx_dict = get_xjxx_dict(cookie)
                gl.set_value("xjxx_dict", xjxx_dict)
                gl.set_value("user_info",
                             {"stu_name": xjxx_dict["姓名"], "username": username, "password": password,
                              "cookie": cookie})
                free_course_list, yxkc_list = get_free_course_list_and_yxkc_list(cookie)
                gl.set_value("free_course_list", free_course_list)
                gl.set_value("yxkc_list", yxkc_list)
            except:
                return 0
        else:
            logs.logs_print("cookie登录")
            cookie = str(self.cookie_lineEdit.text())
            self.login_pushButton.setEnabled(False)
            self.login_status_label.setText(
                '''<html><head/><body><p align="center"><span style=" color:#00aa00;">登录成功</span></p></body></html>''')
            xjxx_dict = get_xjxx_dict(cookie)
            gl.set_value("xjxx_dict", xjxx_dict)
            gl.set_value("user_info", {"stu_name": xjxx_dict["姓名"], "cookie": cookie})
            free_course_list, yxkc_list = get_free_course_list_and_yxkc_list(cookie)
            gl.set_value("free_course_list", free_course_list)
            gl.set_value("yxkc_list", yxkc_list)

        logs.logs_print("登录成功，cookie为 " + cookie)
